package com.campus.backend.controller;

import com.campus.backend.bean.notice.CreateNoticeBean;
import com.campus.backend.bean.notice.NoticeQueryParam;
import com.campus.backend.bean.notice.UpdateNoticeBean;
import com.campus.backend.service.NoticeService;
import com.campus.backend.vo.notice.NoticeVo;
import com.campus.common.bean.common.Resp;
import com.campus.common.bean.common.Pagination;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 公告表 前端控制器
 * </p>
 *
 * @author ZhaoYuJie
 * @since 2025-07-15
 */
@RestController
@RequestMapping("/notice")
@Api(tags = "公告管理")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @GetMapping("/list")
    @ApiOperation("分页查询公告列表")
    public Resp<Pagination<NoticeVo>> getNoticeListPage(NoticeQueryParam param) {
        PageInfo<NoticeVo> pageInfo = noticeService.getNoticeListPage(param);
        return Resp.successPageable(pageInfo);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据ID获取公告详情")
    public Resp<NoticeVo> getNoticeById(@ApiParam("公告ID") @PathVariable Integer id) {
        NoticeVo noticeVo = noticeService.getNoticeById(id);
        return Resp.success(noticeVo);
    }

    @PostMapping
    @ApiOperation("创建公告")
    public Resp<Integer> createNotice(@Valid @RequestBody CreateNoticeBean createNoticeBean) {
        Integer noticeId = noticeService.createNotice(createNoticeBean);
        return Resp.success(noticeId);
    }

    @PutMapping
    @ApiOperation("更新公告")
    public Resp<Void> updateNotice(@Valid @RequestBody UpdateNoticeBean updateNoticeBean) {
        noticeService.updateNotice(updateNoticeBean);
        return Resp.success();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除公告")
    public Resp<Void> deleteNotice(@ApiParam("公告ID") @PathVariable Integer id) {
        noticeService.deleteNotice(id);
        return Resp.success();
    }

    @DeleteMapping("/batch")
    @ApiOperation("批量删除公告")
    public Resp<Void> batchDeleteNotice(@ApiParam("公告ID列表") @RequestBody List<Integer> ids) {
        noticeService.batchDeleteNotice(ids);
        return Resp.success();
    }

    @PutMapping("/{id}/status/{status}")
    @ApiOperation("更新公告状态")
    public Resp<Void> updateNoticeStatus(
            @ApiParam("公告ID") @PathVariable Integer id,
            @ApiParam("状态：1-启用，0-禁用") @PathVariable Integer status) {
        noticeService.updateNoticeStatus(id, status);
        return Resp.success();
    }

    @PutMapping("/{id}/top/{isTop}")
    @ApiOperation("更新公告置顶状态")
    public Resp<Void> updateNoticeTop(
            @ApiParam("公告ID") @PathVariable Integer id,
            @ApiParam("是否置顶：1-置顶，0-不置顶") @PathVariable Integer isTop) {
        noticeService.updateNoticeTop(id, isTop);
        return Resp.success();
    }
}
